package hibernateDAOS;

import hibernate.HibernateUtil;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;


import beans.Movimiento;

public class MovimientoDAO {
	
	private static MovimientoDAO instancia = null;
	private static SessionFactory sf = null;

	public static MovimientoDAO getInstancia(){
		if(instancia == null){
			sf = HibernateUtil.getSessionFactory();
			instancia = new MovimientoDAO();
		} 
		return instancia;
	}
	
	public void guardarMovimiento(Movimiento m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.save(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void eliminarMovimiento(Movimiento m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.delete(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	public void modificarMovimiento(Movimiento m){
		Session session = sf.openSession();
		session.beginTransaction();
		session.saveOrUpdate(m);
		session.flush();
		session.getTransaction().commit();
		session.close();
	}

	@SuppressWarnings("unchecked")
	public List<Movimiento> obtenerMovimientos(){
		Session session = sf.openSession();
		Query q = session.createQuery("from Movimientos");
		List<Movimiento> movimientos = q.list();
		session.close();
		return movimientos;
	}
}




